//
//  WmcCloudAlbumTabBarVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/24.
//

import UIKit
import YPBedrock

// MARK: - 扩展WmcPersonCloudAlbumTabBarVC + WmcNavigatorProtocol

extension WmcPersonCloudAlbumTabBarVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.personCloudAlbum]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcPersonCloudAlbumTabBarVM()
        if let viewModel = parms.context as? WmcPersonCloudAlbumTabBarVM {
            vm = viewModel
        }
        let vc = WmcPersonCloudAlbumTabBarVC(vm)
        vc.teamAblum = vm.teamAblum
        return vc
    }
}

// MARK: - 扩展WmcPersonCloudAlbumTabBarVC + WmcVMProtocol

extension WmcPersonCloudAlbumTabBarVC: WmcVMProtocol {
    typealias VMType = WmcPersonCloudAlbumTabBarVM
}

// MARK: - [WmcCloudAlbumTabbarVC]

public extension WmcButtonItemType {
    /// 按钮类型
    enum PersonCloudAlbumTabBarItem {
        /// 全部照片
        case allPhoto

        /// 分类相册
        case classifyAlbum

        /// 设置
        case setting

        /// 自定义
        case custom(childVC: UIViewController,
                    childTitle: String,
                    normalImage: UIImage,
                    selectedImage: UIImage)
    }
}

// MARK: - [WmcPersonCloudAlbumTabBarVC]

open class WmcPersonCloudAlbumTabBarVC: WmcCloudAlbumBaseTabBarVC {
    // Lifecycle

    override open func viewDidLoad() {
        super.viewDidLoad()
    }

    // Open

    override open func makeUI() {
        // 现组装默认视图
    }

    override open func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        super.tabBar(tabBar, didSelect: item)
    }

    // Internal

    var teamAblum: WmcMediaTeamAlbumModel? {
        didSet {
            // 现组装默认视图
            var defaultList: [WmcButtonItemType.PersonCloudAlbumTabBarItem] = [
                .allPhoto, .classifyAlbum,
            ]

            // 询问外部代理
            if let newList = persionCloudAlbumDelegate?.tabBarItemList(.current, defaultList: defaultList) {
                defaultList = newList
            }
            // 添加tabBar
            addItemList(caseList: defaultList)
        }
    }

    /// 外部代理
    var persionCloudAlbumDelegate: WmcPersionCloudAlbumTabBarDelegate? {
        return YPWmcManager.shared.uiDelegate.personCloudAlbumTabBarDelegate
    }

    /// 转换成按钮实体
    func addItemList(caseList: [WmcButtonItemType.PersonCloudAlbumTabBarItem]) {
        caseList.forEach { cas in
            switch cas {
            case .allPhoto:
                let vm = WmcCloudAllPhotoVM()
                vm.title = "我的云相册"
                vm.teamAblum = self.viewModel.teamAblum
                addCloudAlbumChildVC(WmcCloudAllPhotoVC(vm),
                                     childTitle: "全部照片",
                                     normalImage: "cloud_tabbar_all_photo_no".wmc_image,
                                     selectedImage: "cloud_tabbar_all_photo_se".wmc_image)
            case .classifyAlbum:
                let vm = WmcCloudClassifyAlbumVM()
                vm.title = "分类相册"
                vm.teamAblum = self.viewModel.teamAblum
                addCloudAlbumChildVC(WmcCloudClassifyAlbumVC(vm),
                                     childTitle: "分类相册",
                                     normalImage: "cloud_tabbar_classify_album_no".wmc_image,
                                     selectedImage: "cloud_tabbar_classify_album_se".wmc_image)
            case .setting:
                let vm = WmcCloudMineVM()
                vm.showMode = .setting
                addCloudAlbumChildVC(WmcCloudMineVC(vm),
                                     childTitle: "设置",
                                     normalImage: "cloud_tabbar_setting_no".wmc_image,
                                     selectedImage: "cloud_tabbar_setting_se".wmc_image)
            case let .custom(childVC: childVC, childTitle: childTitle, normalImage: normalImage, selectedImage: selectedImage):
                addCloudAlbumChildVC(childVC,
                                     childTitle: childTitle,
                                     normalImage: normalImage,
                                     selectedImage: selectedImage)
            }
        }
    }
    
    open override func addCloudAlbumChildVC(_ childVC: UIViewController, childTitle: String, normalImage: UIImage, selectedImage: UIImage) {
        addChildVC(childVC, childTitle: childTitle, normalImage: normalImage, selectedImage: selectedImage) { [weak self] in
            guard let weakSelf = self else { return }
            if weakSelf.viewModel.isBackToDismiss {
                weakSelf.dismiss(animated: true)
            }else{
                weakSelf.rootPopVC()
            }
        }
    }
}
